package com.lzy.cloud.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.CarChassisDto;
import com.lzy.cloud.goods.facade.CarChassisFacade;
import com.lzy.cloud.goods.request.CarCategoryRequest;
import com.lzy.cloud.goods.request.CarChassisRequest;
import com.lzy.cloud.goods.response.CarCategoryResponse;
import com.lzy.cloud.goods.response.CarChassisResponse;
import com.lzy.cloud.goods.service.CarChassisService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2018/6/11.
 */
@RestController
@RequestMapping("/goods/car/chassis")
public class CarChassisController {

    @Autowired
    CarChassisService carChassisService;

    @Autowired
    CarChassisFacade carChassisFacade;


    /**
     * 新增底盘品牌
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody(required = false) CarChassisRequest request){
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = carChassisService.add(DozerUtils.map(request, CarChassisDto.class));
        Ensure.that(ok).isTrue("00040001");
        return new Result();
    }

    /**
     * 删除底盘品牌
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del/{ids}",method = RequestMethod.GET)
    public Result del(@PathVariable("ids") String ids){
        Boolean ok = carChassisFacade.del(ids);
        Ensure.that(ok).isTrue("00040002");
        return new Result();
    }

    /**
     * 编辑底盘品牌
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result update(@RequestBody CarChassisRequest request){
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = carChassisService.update(DozerUtils.map(request,CarChassisDto.class));
        Ensure.that(ok).isTrue("00040003");
        return new Result();
    }

    /**
     * 分页查询底盘品牌列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result selectList(CarChassisRequest request){
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<CarChassisDto> pageInfo = carChassisService.selectList(pager,DozerUtils.map(request,CarChassisDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), CarChassisResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 根据id查询底盘品牌详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public Result selectList(@PathVariable Long id){
        CarChassisDto carChassisDto = carChassisService.selectById(id);
        Ensure.that(carChassisDto).isNotNull("00040004");
        return new Result(DozerUtils.map(carChassisDto, CarChassisResponse.class));
    }

    /**
     * 启用/禁用
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/status",method = RequestMethod.POST)
    public Result status(CarChassisRequest request){
        Ensure.that(request).isNotNull("00010001");
        CarChassisDto dto = new CarChassisDto();
        dto.setId(request.getId());
        dto.setStatus(request.getStatus());
        Boolean ok = carChassisService.update(dto);
        Ensure.that(ok).isTrue("00040005");
        return new Result();
    }
}
